package com.im.audio;

import java.io.IOException;

import android.media.MediaRecorder;

import com.core.sdk.core.LogUtil;
import com.im.util.PathUtil;

public class RecorderManager {
	private static final String tag = null;

	MediaRecorder mMediaRecorder = null;

	private final String fileSavePath ;
	boolean inited = false;
	public RecorderManager() {
		fileSavePath = PathUtil.getCacheDir()+"audio_"+System.currentTimeMillis()+".amr";
		try {
			initRecorder();
		} catch (Exception e) {
			inited = false;
			e.printStackTrace();
		}
	}
	
	/** 录制时间   */
	private String recorderTime = "";
	private long startRecorderTime = -1;
	private void initRecorder() throws IllegalStateException, IOException {
		if(mMediaRecorder != null){
			mMediaRecorder.reset();
			mMediaRecorder.release();
			mMediaRecorder = null;
		}
		mMediaRecorder = new MediaRecorder();
		mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
		mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		mMediaRecorder.setOutputFile(fileSavePath);
		mMediaRecorder.prepare();
		inited = true;
	}
	
	public void shutdown(){
		long seconds = (System.currentTimeMillis()-startRecorderTime)/1000;
		recorderTime = seconds+"\"";
		mMediaRecorder.stop();
		mMediaRecorder.release();
		mMediaRecorder = null;
		inited = false;
		
	}
	
	public void start(){
		if(!inited){
			LogUtil.w(tag, "mMediaRecorder can't inited!");
			return ;
		}
		mMediaRecorder.start();
		startRecorderTime = System.currentTimeMillis();
	}
	
	public String getSavePath(){
		return this.fileSavePath;
	}

	/** 获得录音的时间(秒) */
	public String getRecorderTime() {
		return recorderTime;
	}
	
}
